package com.newcoin.chat.service.impl;

import com.newcoin.chat.beans.ComparatorTime;
import com.newcoin.chat.beans.entity.FriendMsgEntity;
import com.newcoin.chat.beans.entity.GroupMsgEntity;
import com.newcoin.chat.beans.vo.FriendOfflineMsgVo;
import com.newcoin.chat.beans.vo.GroupOfflineMsgVo;
import com.newcoin.chat.dao.ChatDao;
import com.newcoin.chat.service.ChatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
@Slf4j
public class ChatServiceImpl implements ChatService {
    @Autowired
    private ChatDao dao;

    /**
     * 获取离线消息
     *
     * @param userId
     * @return
     * @throws Exception
     */
    @Override
    public List getOfflineMsg(long userId) throws Exception {
        log.info("获取离线消息入参userId={}", userId);
        List<FriendOfflineMsgVo> friendMsgList = dao.getFriendOfflineMsg(userId);
        log.info("friendMsgList={}", friendMsgList);
        List<GroupOfflineMsgVo> groupMsgList = dao.getGroupOfflineMsg(userId);
        log.info("groupMsgList={}", groupMsgList);
        List list = new ArrayList();
        friendMsgList.forEach(f -> {
            if (f.getIsOpenBurnDown() != 0)
                f.setFriendMsgContent(burnAfterReadingStatusSwitch(f.getIsOpenBurnDown()));
            log.info("friendMsgContent={}", f.getFriendMsgContent());
            list.add(f);
        });
        groupMsgList.forEach(g -> {
            if (g.getIsOpenBurnDown() != 0)
                g.setGroupMsgContent(burnAfterReadingStatusSwitch(g.getIsOpenBurnDown()));
            log.info("GroupMsgContent={}", g.getGroupMsgContent());
            list.add(g);
        });
        Collections.sort(list, new ComparatorTime());//正序排序
        Collections.reverse(list);//反转排序(倒序)
        log.info("倒叙后 list={}", list);
        return list;
    }

    /**
     * 朋友消息插入数据库
     *
     * @param friendMsgEntity
     * @throws Exception
     */
    @Override
    public void addFriendMsg(FriendMsgEntity friendMsgEntity) throws Exception {
        log.info("FriendMsgEntity={}", friendMsgEntity);
        dao.addFriendMsg(friendMsgEntity);
    }

    /**
     * 更新朋友发来未读的消息记录
     *
     * @param userId
     * @param friendId
     * @param msgId
     * @throws Exception
     */
    @Override
    public void updateFriendLastMsfInfo(long userId, long friendId, long msgId, int num) throws Exception {
        log.info("更新朋友发来未读的消息记录入参userId={},FriendId={},msgId={}", userId, friendId, msgId);
        dao.updateFriendLastMsgNumAndLastMsgId(userId, friendId, msgId, num);
    }

    /**
     * 获取群成员
     *
     * @param groupId
     * @return
     * @throws Exception
     */
    @Override
    public List<Long> getGroupMember(Long groupId) throws Exception {
        log.info("group={}", groupId);
        List<Long> groupMember = dao.getGroupMember(groupId);
        return groupMember;
    }

    /**
     * 群消息插入数据库
     *
     * @param groupMsgEntity
     * @throws Exception
     */
    @Override
    public void addGroupMsg(GroupMsgEntity groupMsgEntity) throws Exception {
        log.info("groupMsgEntity={}", groupMsgEntity);
        dao.addGroupMsg(groupMsgEntity);
    }

    /**
     * 更新群组发来不在线群员未读的消息条数及最后一条的消息id
     *
     * @param userId
     * @param groupId
     * @param msgId
     * @throws Exception
     */
    @Override
    public void updateGroupLastMsfInfo(long groupId, long userId, long msgId) throws Exception {
        log.info("更新群组发来不在线群员未读的消息条数及最后一条的消息id入参groupId={},userId={},msgId={}", groupId, userId, msgId);
        dao.updateGroupLastMsgNumAndLastMsgId(groupId, userId, msgId);
    }


    /**
     * 阅后即焚状态选择器
     *
     * @param state
     * @return
     */
    private String burnAfterReadingStatusSwitch(int state) {
        switch (state) {
            case 0:
                return "关闭阅后即焚";
            case 1:
                return "开启阅后即焚";
            case 2:
                return "开启10秒后焚毁";
            case 3:
                return "开启5分钟后焚毁";
            case 4:
                return "开启1小时后后焚毁";
            case 5:
                return "开启24小时后焚毁";
            default:
                return "关闭阅后即焚";
        }
    }
}
